
import 'package:flutter/material.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class WidgetProfileRich extends StatelessWidget {
  const WidgetProfileRich({super.key, required this.text});

  final String text;

  @override
  Widget build(BuildContext context) {
    final List<InlineSpan> spans = [];
    final hStyle = TextStyle(
      fontSize: 12.sp,
      fontWeight: FontWeight.w600,
      color: AppColor.primary,
      // height: 1.55,
    );
    final style = TextStyle(
        fontSize: 12.sp,
        fontWeight: FontWeight.w400,
        color: AppColor.textPrimary.withOpacity(0.5));
    // 匹配 {{xxx}} 这种标记
    final regex = RegExp(r'{(.*?){');
    int lastMatchEnd = 0;

    for (final match in regex.allMatches(text)) {
      // 添加普通文本
      if (match.start > lastMatchEnd) {
        spans.add(TextSpan(
          text: text.substring(lastMatchEnd, match.start),
          style: style,
        ));
      }
      // 添加高亮文本
      spans.add(TextSpan(
        text: match.group(1), // 拿到 {{xxx}} 中的 xxx
        style: hStyle,
      ));
      lastMatchEnd = match.end;
    }

    // 添加最后剩余的普通文本
    if (lastMatchEnd < text.length) {
      spans.add(TextSpan(
        text: text.substring(lastMatchEnd),
        style: style,
      ));
    }

    return RichText(text: TextSpan(style: style, children: spans));
  }
}
